package logic

import (
	"context"
	"github.com/jinzhu/copier"
	"my-zero-tiktok/common/xerr"

	"my-zero-tiktok/apps/video/pb/pb"
	"my-zero-tiktok/apps/video/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetVideoListByUserIDLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetVideoListByUserIDLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetVideoListByUserIDLogic {
	return &GetVideoListByUserIDLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetVideoListByUserID 根据用户id获取视频
func (l *GetVideoListByUserIDLogic) GetVideoListByUserID(in *pb.UserIDRequest) (*pb.VideoListResponse, error) {
	videos, err := l.svcCtx.VideoModel.FindListByUserID(l.ctx, in.UserId)
	if err != nil {
		return nil, xerr.New(xerr.VideoGetListError)
	}

	list := make([]*pb.VideoItem, len(videos))
	for i, item := range videos {
		var newItem pb.VideoItem
		_ = copier.Copy(&newItem, item)
		list[i] = &newItem
	}
	return &pb.VideoListResponse{List: list}, nil
}
